package com.example.demo.util;

import com.alibaba.fastjson2.util.TypeUtils;
import com.google.common.collect.Maps;

import java.util.Map;


/**
 * ThreadContext
 *
 * @author hao.chen
 * @date 2022-10-13 20:12
 */
public class ThreadContext {

    private static final ThreadLocal<Map<String, Object>> MAP_THREAD_LOCAL = ThreadLocal.withInitial(Maps::newHashMap);

    public static void remove() {
        MAP_THREAD_LOCAL.remove();
    }

    public static void put(String k, Object v) {
        MAP_THREAD_LOCAL.get().put(k, v);
    }

    public static Object get(String k) {
        return MAP_THREAD_LOCAL.get().get(k);
    }

    public static <T> T getObject(String key, Class<T> clazz) {
        Object obj = get(key);
        return TypeUtils.cast(obj, clazz);
    }
}
